package pe.edu.upc.dsd.l33t.service.impl;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pe.edu.upc.dsd.l33t.dao.BookDao;
import pe.edu.upc.dsd.l33t.model.Book;
import pe.edu.upc.dsd.l33t.service.BookService;

import com.google.gson.Gson;

@Path("/books")
@Service
public class BookServiceImpl implements BookService {
	
	@Autowired
	private BookDao bookDao;

	@GET
	@Path("/{isbn}")
	@Produces("text/plain")
	@Consumes("text/plain")
	public Response find(@PathParam("isbn") String isbn){
		
		Book book;
		Gson gson = new Gson();
		
		System.out.println("isbn: " + isbn);
		
		book = bookDao.find(isbn);
		if (book == null) {
			return Response.status(Status.INTERNAL_SERVER_ERROR)
					.entity("El libro no existe").build();
		}
		
		return Response.ok(gson.toJson(book)).build();
	}
	
}
